﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Editor;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Properties;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationAlignSelectedShprossesByAxes : IOperation
    {
        private bool isContextMenu = true;
        private ComponentAlign componentAlign;
        private ModelControl modelControl;
        public OperationAlignSelectedShprossesByAxes()
        {
        }

        public OperationAlignSelectedShprossesByAxes(ComponentAlign align) {
            this.componentAlign = align;
            this.isContextMenu = false;
        }

        #region IOperation Members

        public string Name
        {
            get { return "Выравнивание профилей по осям"; }
        }

        public void Execute(ModelControl modelControl)
        {

            this.modelControl = modelControl;
            if(isContextMenu) {
                ContextMenuStrip menuStrip = new ContextMenuStrip();

                EventHandler handler = new EventHandler(OnItemClick);
                ToolStripItem horizontal = new ToolStripMenuItem("По горизонтали", Resources.Horizontal.ToBitmap(), handler);
                horizontal.Tag = ComponentAlign.Horizontal;
                menuStrip.Items.Add(horizontal);

                ToolStripItem vertical = new ToolStripMenuItem("По вертикали", Resources.Vertical.ToBitmap(), handler);
                vertical.Tag = ComponentAlign.Vertical;
                menuStrip.Items.Add(vertical);

                menuStrip.Show(Cursor.Position);
            } else
                this.Align(this.componentAlign);
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ComponentAlign align = (ComponentAlign)item.Tag;
            this.Align(align);

        }

        private void Align(ComponentAlign align) {
            foreach(IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
                if(selectedComponent is Shpross) {
                    IComponentAction alignAction = new ActionAlignSingleShpross(align, false);
                    alignAction.Action(selectedComponent);
                }

            this.modelControl.UpdateModel();
            this.modelControl.UpdateModel();
        }
        #region IOperation Members


        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        #endregion

        #region IOperation Members


        public System.Drawing.Image Thumbnail
        {
            get { return Resources.AlignShprossesByAxes.ToBitmap(); }
        }

        #endregion

        #region IOperation Members


        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
